package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController{
    @Autowired
    private EmployeeService employeeService ;
    //登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpServletRequest request){
        //1.获取登录密码 进行MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2根据用户名查询用户信息，判断用户是否存在
        //2.1创造条件查询构造器
        //select * from employee

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //where uername="admin"
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);
        //3.存在了 进行 密码对比 不存在 返回登录失败
        if(emp == null){
            return R.error("你输入的用户名不存在");
        }
        //4.密码对比 不一致 登录失败
        if (!emp.getPassword().equals(password)){
            return R.error("你输入的密码有误 请重新输入");
        }
        //5.密码一致查询当前用户是否被禁用
        //6.禁用登录失败 启用 登陆成功
        if (emp.getStatus()==0){
            return R.error("你的账号已经被禁用 请联系管理员");
        }
        //7.保存用户 的id到session中
        request.getSession().setAttribute("employee",emp.getId());
        //8.返回登录对象给前端
        return R.success(emp);
    }

    //退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清空session中保存的当前登录的员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //http://localhost:8080/employee/page?page=1&pagesize=4&name=nn

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //1.创建分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //12.创建查询条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //3.添加查询条件
        //like 方法 有三个参数 第-个参数boolean类型 如果是true 后面查询有条件成立拼接到查询语句中
        //如果尽foals 不成立 后市的查询条件不拼接到查询语句中
        //where name like "%hame"
        //lambdaQueryWrapperllike(name!=null,Employee:getName,name);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //14.执行分页查询
        employeeService.page(pageInfo,lambdaQueryWrapper);
        //15,返回在询结果
        return R.success(pageInfo);
    }

    //添加员工
    /**
     * 添加员工信息
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> sava(@RequestBody Employee employee,HttpServletRequest request){
        //设置初始密码123456 需要MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加员工成功");
    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee ,HttpServletRequest request){
        Long empId = (Long) request.getSession().getAttribute("employee");
        log.info("empId:{}",empId);
        employee.setUpdateUser(empId);
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");

    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id ){
        log.info("根据id查询员工信息"+id);
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到你要查的用户");
    }
}
